<?php

class XMLDataParser extends DataParser {

    public function parseItem($item) {
        $event = new NewsItem();
        $event->setAuthor($item['author']);
        $event->setDescription($item['description']);
        $event->setContent($item['description']);
        $event->setPubDate(new DateTime($item['createDate']));
        $event->setId($item['id']);
        $event->setTitle($item['title']);
        $event->setLink('http://www.ntpc.gov.tw/ap/newcalendar/newcalendar_info.jsp?id=' . $item['id']);
        return $event;
    }

    public function parseData($data) {
        $xml = simplexml_load_string($data);
        $json = json_encode($xml);
        $array = json_decode($json, TRUE);
        $arrRow = $array['row'];
        $eventArr = array();
        foreach ($arrRow as $arr) {
            $eventArr[] = $this->parseItem($arr);
        }
        $this->setTotalItems(count($eventArr));
        return $eventArr;
    }
}
